package com.dave.gd.core.base.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dave.gd.common.util.JSONUtils;
import com.dave.gd.core.vm.EasyUIResultModel;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public abstract class AbstractBaseAction extends ActionSupport {
    private static final long serialVersionUID = -3749536887775887160L;
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    protected final String contextPath = ServletActionContext.getRequest().getContextPath();
    protected String next;
    protected String forward;
    protected String redirect;
    
    protected int page = 1;
    protected int rows = 10;
    
    protected String searchValue;//搜索的类型
    protected String searchName;//类型对应的值
    
    protected EasyUIResultModel easyUIResultModel = new EasyUIResultModel();
    
    protected final String SUCCESS = "success";
    protected final String MAIN = "main";
    protected final String NEXT = "next";
    protected final String FORWARD = "forward";
    protected final String REDIRECT = "redirect";
    protected final String DOWNLOAD = "download";
    
    public String getForward() {
        return forward;
    }
    
    public void setForward(String forward) {
        this.forward = forward;
    }
    
    public String getRedirect() {
        return redirect;
    }
    
    public void setRedirect(String redirect) {
        this.redirect = redirect;
    }
    
    public HttpServletRequest getRequest() {
        return ServletActionContext.getRequest();
    }
    
    public ActionContext getContext() {
        return ActionContext.getContext();
    }
    
    public HttpSession getSession() {
        return this.getRequest().getSession();
    }
    
    public Map<String, Object> getSessionToMap() {
        return ActionContext.getContext().getSession();
    }
    
    public void clearSession() {
        this.getRequest().getSession().invalidate();
    }
    
    /**
     * 获取HttpServletResponse对象
     * 
     * @return
     */
    public HttpServletResponse getResponse() {
        return ServletActionContext.getResponse();
    }
    
    public <T> void writeJson(List<T> beans) {
        
        writePlainText(JSONUtils.listToJsonString(beans, ""));
    }
    
    public void writeJson(Object obj) {
        
        writePlainText(JSONUtils.objectToDateFormatJson(obj));
    }
    
    /**
     * 功能：输出文本流到页面
     * 
     * @param text
     *            <code>String</code>
     */
    protected void writePlainText(String text) {
        try {
            getResponse().setContentType("text/plain;charset=UTF-8");
            Writer writer = getResponse().getWriter();
            writer.write(text);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    protected void writeXml(String xml) {
        try {
            this.getResponse().setContentType("text/xml;charset=utf-8");
            Writer writer = this.getResponse().getWriter();
            writer.write(xml);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    protected String decode(String str) {
        if (StringUtils.isEmpty(str)) {
            return str;
        }
        try {
            return URLDecoder.decode(str, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }
    
    /**
     * 
     * .后台返回数据为空的时候，调用writjson返回。
     * 
     * @author yangdafu
     * 2015年1月21日 yangdafu
     */
    public void NullValReturn() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("msg", "后台返回为空！");
        map.put("success", "false");
        map.put("total", "0");
        map.put("rows", "");
        writeJson(map);
    }
    
    public int getPage() {
        return page;
    }
    
    public void setPage(int page) {
        this.page = page;
    }
    
    public int getRows() {
        return rows;
    }
    
    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getSearchValue() {
        return searchValue;
    }

    public void setSearchValue(String searchValue) {
        this.searchValue = searchValue;
    }

    public String getSearchName() {
        return searchName;
    }

    public void setSearchName(String searchName) {
        this.searchName = searchName;
    }

    public EasyUIResultModel getEasyUIResultModel() {
        return easyUIResultModel;
    }

    public void setEasyUIResultModel(EasyUIResultModel easyUIResultModel) {
        this.easyUIResultModel = easyUIResultModel;
    }
    
}
